package com.itheima.controller;

import com.itheima.pojo.Emp;
import com.itheima.pojo.Result;
import com.itheima.service.EmpService;
import com.itheima.utils.JwtUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import java.util.HashMap;
import java.util.Map;

@RestController
@Slf4j
public class LoginController {

    @Autowired
    private EmpService empService;

    @PostMapping("/login")
    public Result login(@RequestBody Emp emp) {
        log.info("用户登录，接收到的参数为：{}", emp);
        Emp employee = empService.login(emp);

        if (employee != null) {//登陆成功，生成令牌
            Map<String, Object> claims = new HashMap<>();
            claims.put("id", employee.getId());
            claims.put("name", employee.getName());
            claims.put("username", employee.getUsername());
            String token = JwtUtils.generateJwt(claims);
            return Result.success(token);
        } else {
            return Result.error("NOT_LOGIN");
        }
    }
}
